home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS09.ADF / MicroEMACS / region.c < prev    next >
C/C++ Source or Header  |  1986-05-22  |  7KB  |  203 lines

  1. /*
  2.  * The routines in this file
  3.  * deal with the region, that magic space
  4.  * between "." and mark. Some functions are
  5.  * commands. Some functions are just for
  6.  * internal use.
  7.  */
  8. #include        <stdio.h>
  9. #include        "ed.h"
  10.  
  11. /*
  12.  * Kill the region. Ask "getregion"
  13.  * to figure out the bounds of the region.
  14.  * Move "." to the start, and kill the characters.
  15.  * Bound to "C-W".
  16.  */
  17. killregion(f, n)
  18. {
  19.         register int    s;
  20.         REGION          region;
  21.  
  22.         if ((s=getregion(®ion)) != TRUE)
  23.                 return (s);
  24.         if ((lastflag&CFKILL) == 0)             /* This is a kill type  */
  25.                 kdelete();                      /* command, so do magic */
  26.         thisflag |= CFKILL;                     /* kill buffer stuff.   */
  27.         curwp->w_dotp = region.r_linep;
  28.         curwp->w_doto = region.r_offset;
  29.     mlwrite( "[Region Cut]" );
  30.         return (ldelete(region.r_size, TRUE));
  31. }
  32.  
  33. /*
  34.  * Copy all of the characters in the
  35.  * region to the kill buffer. Don't move dot
  36.  * at all. This is a bit like a kill region followed
  37.  * by a yank. Bound to "M-W".
  38.  */
  39. copyregion(f, n)
  40. {
  41.         register LINE   *linep;
  42.         register int    loffs;
  43.         register int    s;
  44.         REGION          region;
  45.  
  46.         if ((s=getregion(®ion)) != TRUE)
  47.                 return (s);
  48.         if ((lastflag&CFKILL) == 0)             /* Kill type command.   */
  49.                 kdelete();
  50.         thisflag |= CFKILL;
  51.         linep = region.r_linep;                 /* Current line.        */
  52.         loffs = region.r_offset;                /* Current offset.      */
  53.     mlwrite( "[Region Copied]" );
  54.         while (region.r_size--) {
  55.                 if (loffs == llength(linep)) {  /* End of line.         */
  56.                         if ((s=kinsert('\n')) != TRUE)
  57.                                 return (s);
  58.                         linep = lforw(linep);
  59.                         loffs = 0;
  60.                 } else {                        /* Middle of line.      */
  61.                         if ((s=kinsert(lgetc(linep, loffs))) != TRUE)
  62.                                 return (s);
  63.                         ++loffs;
  64.                 }
  65.         }
  66.         return (TRUE);
  67. }
  68.  
  69. /*
  70.  * Lower case region. Zap all of the upper
  71.  * case characters in the region to lower case. Use
  72.  * the region code to set the limits. Scan the buffer,
  73.  * doing the changes. Call "lchange" to ensure that
  74.  * redisplay is done in all buffers. Bound to 
  75.  * "C-X C-L".
  76.  */
  77. lowerregion(f, n)
  78. {
  79.         register LINE   *linep;
  80.         register int    loffs;
  81.         register int    c;
  82.         register int    s;
  83.         REGION          region;
  84.  
  85.         if ((s=getregion(®ion)) != TRUE)
  86.                 return (s);
  87.         lchange(WFHARD);
  88.         linep = region.r_linep;
  89.         loffs = region.r_offset;
  90.         while (region.r_size--) {
  91.                 if (loffs == llength(linep)) {
  92.                         linep = lforw(linep);
  93.                         loffs = 0;
  94.                 } else {
  95.                         c = lgetc(linep, loffs);
  96.                         if (c>='A' && c<='Z')
  97.                                 lputc(linep, loffs, c+'a'-'A');
  98.                         ++loffs;
  99.                 }
  100.         }
  101.         return (TRUE);
  102. }
  103.  
  104. /*
  105.  * Upper case region. Zap all of the lower
  106.  * case characters in the region to upper case. Use
  107.  * the region code to set the limits. Scan the buffer,
  108.  * doing the changes. Call "lchange" to ensure that
  109.  * redisplay is done in all buffers. Bound to 
  110.  * "C-X C-L".
  111.  */
  112. upperregion(f, n)
  113. {
  114.         register LINE   *linep;
  115.         register int    loffs;
  116.         register int    c;
  117.         register int    s;
  118.         REGION          region;
  119.  
  120.         if ((s=getregion(®ion)) != TRUE)
  121.                 return (s);
  122.         lchange(WFHARD);
  123.         linep = region.r_linep;
  124.         loffs = region.r_offset;
  125.         while (region.r_size--) {
  126.                 if (loffs == llength(linep)) {
  127.                         linep = lforw(linep);
  128.                         loffs = 0;
  129.                 } else {
  130.                         c = lgetc(linep, loffs);
  131.                         if (c>='a' && c<='z')
  132.                                 lputc(linep, loffs, c-'a'+'A');
  133.                         ++loffs;
  134.                 }
  135.         }
  136.         return (TRUE);
  137. }
  138.  
  139. /*
  140.  * This routine figures out the
  141.  * bounds of the region in the current window, and
  142.  * fills in the fields of the "REGION" structure pointed
  143.  * to by "rp". Because the dot and mark are usually very
  144.  * close together, we scan outward from dot looking for
  145.  * mark. This should save time. Return a standard code.
  146.  * Callers of this routine should be prepared to get
  147.  * an "ABORT" status; we might make this have the
  148.  * conform thing later.
  149.  */
  150. getregion(rp)
  151. register REGION *rp;
  152. {
  153.         register LINE   *flp;
  154.         register LINE   *blp;
  155.         register int    fsize;
  156.         register int    bsize;
  157.  
  158.         if (curwp->w_markp == NULL) {
  159.                 mlwrite("No mark set in this window");
  160.                 return (FALSE);
  161.         }
  162.         if (curwp->w_dotp == curwp->w_markp) {
  163.                 rp->r_linep = curwp->w_dotp;
  164.                 if (curwp->w_doto < curwp->w_marko) {
  165.                         rp->r_offset = curwp->w_doto;
  166.                         rp->r_size = curwp->w_marko-curwp->w_doto;
  167.                 } else {
  168.                         rp->r_offset = curwp->w_marko;
  169.                         rp->r_size = curwp->w_doto-curwp->w_marko;
  170.                 }
  171.                 return (TRUE);
  172.         }
  173.         blp = curwp->w_dotp;
  174.         bsize = curwp->w_doto;
  175.         flp = curwp->w_dotp;
  176.         fsize = llength(flp)-curwp->w_doto+1;
  177.         while (flp!=curbp->b_linep || lback(blp)!=curbp->b_linep) {
  178.                 if (flp != curbp->b_linep) {
  179.                         flp = lforw(flp);
  180.                         if (flp == curwp->w_markp) {
  181.                                 rp->r_linep = curwp->w_dotp;
  182.                                 rp->r_offset = curwp->w_doto;
  183.                                 rp->r_size = fsize+curwp->w_marko;
  184.                                 return (TRUE);
  185.                         }
  186.                         fsize += llength(flp)+1;
  187.                 }
  188.                 if (lback(blp) != curbp->b_linep) {
  189.                         blp = lback(blp);
  190.                         bsize += llength(blp)+1;
  191.                         if (blp == curwp->w_markp) {
  192.                                 rp->r_linep = blp;
  193.                                 rp->r_offset = curwp->w_marko;
  194.                                 rp->r_size = bsize - curwp->w_marko;
  195.                                 return (TRUE);
  196.                         }
  197.                 }
  198.         }
  199.         mlwrite("Bug: lost mark");
  200.         return (FALSE);
  201. }
  202.  
  203.